#### "Reanalyzing the Link between Democracy and Economic Development" ####
# journal: International Area Studies Review
# authors: "Pelke, Lars"
# date: 2023-07-24
# written under "R version 4.1.2 (2021-11-01)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set your working directory 
# setwd()


# loading packages

library(countrycode)
library(tidyverse)
library(viridis)
library(ggpubr)
library(texreg)
library(sjPlot)
library(ggeffects)
library(readstata13)
library(imputeTS)
library(stargazer)
library(haven)


#### Import Data ####

# Load VDem data

vdem_data <- readRDS("data/vdem_data.rds")

#### Construct Binary Democracy Measure ####

#### Subset Data ####

data_figure <- vdem_data %>%
  filter(country_name %in% c("Turkey", "Venezuela"))

data_figure_turkey <- data_figure %>%
  filter(year >=1900) %>%
  relocate(country_name, year, dem_ep, dem, dem_bin_EDI) %>%
  filter(country_name == "Turkey")



F_Turkey <- ggplot(data_figure_turkey, aes(x = year)) +
  geom_line(aes(y = v2x_polyarchy)) +
  geom_line(data =data_figure_turkey[data_figure_turkey$year >= 1907  & data_figure_turkey$year <=1909,], 
            aes(x = year, y = v2x_polyarchy), color = "red", size =1) +
  geom_line(data = data_figure_turkey[data_figure_turkey$year >= 1945  & data_figure_turkey$year <=1951,], 
            aes(x = year, y = v2x_polyarchy), color = "red", size =1) +
  geom_line(data = data_figure_turkey[data_figure_turkey$year >= 1961  & data_figure_turkey$year <=1967,], 
            aes(x = year, y = v2x_polyarchy), color = "red", size =1) +
  geom_line(data = data_figure_turkey[data_figure_turkey$year >= 1983  & data_figure_turkey$year <=1988,], 
            aes(x = year, y = v2x_polyarchy), color = "red", size =1) +
  geom_hline(yintercept = 0.5, linetype = "dashed") +
  geom_label(
    label="liberalizing\n autocracy", 
    x=1907,
    y=0.2,
    label.padding = unit(0.55, "lines"), # Rectangle size around label
    label.size = 0.35,
    color = "black",
    fill="#69b3a2"
  ) +
  geom_label(
    label="liberalizing\n autocracy", 
    x=1948,
    y=0.5,
    label.padding = unit(0.55, "lines"), # Rectangle size around label
    label.size = 0.35,
    color = "black",
    fill="#69b3a2"
  ) +
  geom_label(
    label="democratic\n transition", 
    x=1964,
    y=0.65,
    label.padding = unit(0.55, "lines"), # Rectangle size around label
    label.size = 0.35,
    color = "black",
    fill="#AA4371"
  ) +
  geom_label(
    label="democratic\n transition", 
    x=1990,
    y=0.25,
    label.padding = unit(0.55, "lines"), # Rectangle size around label
    label.size = 0.35,
    color = "black",
    fill="#AA4371"
  ) +
  geom_point(aes(y=dem), color = "#453781FF") +
  annotate(
    "text", label = "Binary democracy\n measure (Step 1)",
    x=1960,
    y=0.90, size = 5, colour = "#453781FF" ) +
  geom_point(aes(y=dem_bin_EDI+0.03), color = "#238A8DFF", alpha =0.5, shape = 2) +
  annotate(
    "text", label = "Binary democracy\n measure (Step 2)",
    x=1925,
    y=0.25, size = 5, colour = "#238A8DFF" ) +
  theme_pubr() +
  ylim(0,1.05) +
  labs(y = "Electoral Democracy Index", 
       x = "Year", 
       color = "", 
       title = "Turkey")  +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=16,face="bold"))

## Venezuela ##

data_figure_venezuela <- data_figure %>%
  filter(year >=1900) %>%
  relocate(country_name, year, dem_ep, dem, dem_bin_EDI) %>%
  filter(country_name == "Venezuela") 

F_Venezuela  <- ggplot(data_figure_venezuela, aes(x = year)) +
  geom_line(aes(y = v2x_polyarchy)) +
  geom_line(data =data_figure_venezuela[data_figure_venezuela$year >= 1945  & data_figure_venezuela$year <=1948,], 
            aes(x = year, y = v2x_polyarchy), color = "red", size =1) +
  geom_line(data = data_figure_venezuela[data_figure_venezuela$year >= 1957  & data_figure_venezuela$year <=1960,], 
            aes(x = year, y = v2x_polyarchy), color = "red", size =1) +
  geom_hline(yintercept = 0.5, linetype = "dashed") +
  geom_label(
    label="liberalizing\n autocracy", 
    x=1948,
    y=0.5,
    label.padding = unit(0.55, "lines"), # Rectangle size around label
    label.size = 0.35,
    color = "black",
    fill="#69b3a2"
  ) +
  geom_label(
    label="democratic\n transition", 
    x=1968,
    y=0.5,
    label.padding = unit(0.55, "lines"), # Rectangle size around label
    label.size = 0.35,
    color = "black",
    fill="#AA4371"
  ) +
  geom_point(aes(y=dem), color = "#453781FF") +
  annotate(
    "text", label = "Binary democracy\n measure (Step 1)",
    x=1960,
    y=0.90, size = 5, colour = "#453781FF" ) +
  geom_point(aes(y=dem_bin_EDI+0.03), color = "#238A8DFF", alpha =0.5, shape = 2) +
  annotate(
    "text", label = "Binary democracy\n measure (Step 2)",
    x=1925,
    y=0.25, size = 5, colour = "#238A8DFF" ) +
  theme_pubr() +
  ylim(0,1.05) +
  labs(y = "Electoral Democracy Index", 
       x = "Year", 
       color = "", 
       title = "Venezuela") +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=16,face="bold"))


ggarrange(F_Turkey, F_Venezuela, nrow = 2)


ggsave("outputs/Democratization_operationalization.pdf",  width = 35,
       height = 30,
       units = "cm",
       dpi = 1200)




